

<!-- datagrid部分 -->
<table id="fileup_list"></table>

<!-- 条件查询、按钮 -->
<form  id="fileup_search">
	<table>
		<tr>
			<td>
				<label>原始文件名：</label>
			</td>
			<td>
				<input id="origName"   name="origName" class="easyui-textbox"
				 data-options="prompt:'原始文件名'" style="width:200px">
			</td>
			<td>
				<label>上传日期（之前）：</label>
			</td>
			<td>
				<input   name="createdDate_end"  data-options="prompt:'早于该日期上传'"  class="easyui-datebox" style="width:160px;">
			</td>
			<td>
			 <input type="checkbox" id="ownfile"  name="ownfile"  class="easyui-checkbox"  ><label>只显示本人上传文件</label>
			 </td>
			<td>
			<td>
			    <input type="checkbox" id="rigor"  name="rigor" checked="checked"  style="display: none" > 
				<a href="#" plain="true" id="fileup_search_submit" onclick="javascript:;">查询</a>
				<a href="#" plain="true" id="fileup_search_reset" onclick="javascript:;">重置</a>
			</td>
		</tr>
	</table>
	
	<div style="padding-top:2px;"></div>
	<a href="#" id="fileup_add" onclick="javascript:;">添加</a>
    <a href="#" id="fileup_del" onclick="javascript:;">删除</a>
	<!-- <a href="#"  id="fileup_export"    onclick="javascript:;">数据导出</a> -->
</form>

<!-- 弹出窗口 detail -->
<div id="fileup_popwin" class="easyui-dialog"
	style="width: 500px; height: 400px; padding: 10px 30px;"
	title="文件管理" buttons="#fileup_popwin_buttons"
	data-options="modal:true,closed:true">
	 <h2><label>文件管理</label></h2>
     <form id="fileup_detail_form" class="easyui-form" method="post">
     </form>
</div>
<div id="fileup_popwin_buttons">
	<a href="#" id="fileup_save" onclick="javascript:;">提交保存</a>
</div>

<!-- 弹出窗口 uploadExcel -->
<div id="fileup_import_popwin" class="easyui-dialog"
	style="width: 400px; height: 200px; padding: 10px 30px;"
	title="Excel数据导入" buttons="#fileup_import_buttons"
	data-options="modal:true,closed:true">
	<form id="fileup_import_form" method="post" enctype="multipart/form-data">
		<table  style="width:98%;border-collapse:separate;border-spacing:10px;">
			<tr>
				<td>
					<input id="fileup_import_file" name="upload_file" class="easyui-filebox" 
						style="width: 90%" data-options="
						buttonText:'选择文件'"/>
				</td>
			</tr>
		</table>
	</form>
</div>
<div id="fileup_import_buttons">
    <a href="#" id="fileup_import_button" onclick="javascript:;">数据导入</a>
</div>

<script type="text/javascript">
var  fileup_search= function(){
	var modelname="Fileup";
	var pageName="fileup";
   	var listPage=$("#"+pageName+"_list");
   	var search_form=pageName+"_search"
   	var submit_button=$("#"+pageName+"_search_submit");
   	var reset_button=$("#"+pageName+"_search_reset");
	var export_button=$("#"+pageName+"_export");
   	var popupWin=$("#"+pageName+"_popwin");
  //以下根据id获取按钮元素
	var query_button=$("#"+pageName+"_query");
	var del_button=$("#"+pageName+"_del");
	var import_button=$("#"+pageName+"_add");
  	//Excel导入弹窗
   	var importWin=$("#"+pageName+"_import_popwin");
   	var importForm=$("#"+pageName+"_import_form");
   	var importFile=$("#"+pageName+"_import_file");
   	var importButton=$("#"+pageName+"_import_button");
	//编辑弹窗
	var detailForm=$("#"+pageName+"_detail_form");
   	var popupWin=$("#"+pageName+"_popwin");
   	
   	var columnsAll = new Array();  
    listPage.datagrid({
	            url: 'api/'+modelname+'/search',
	            singleSelect: true,
	            pagination: true,
	            rownumbers:true,
	            toolbar:"#"+search_form,
	            pageSize: 15,
	            pageList: [5, 10, 15, 20, 30, 50, 80, 200],
	            queryParams: form2Json(search_form),　　//关键之处
	            columns:[[{
		            field: "finalName",
		            title: "存盘文件名",
		            width: 450
		        },{
		            field: "origName",
		            title: "原始文件名",
		            align: 'center',
		            width: 360,
		            formatter: function(value, row) {
		            return	"<a href='upload\\"+row.finalName+"'  target='_blank' >"+value+"</a>";
		            }
		        }, 
		        {
		            field: "path",
		            title: "相对路径",
		            align: 'center',
		            width: 100
		        }, 
		        {
		            field: "createdDate",
		            title: "创建日期",
		            align: 'center',
		            width: 100,
		            formatter: function(value, row) {
		            	if(value!=undefined)
		            	return moment(value).format("YYYY-MM-DD");
		            	else
		            	return ""
		            }
		        }, 
		        {
		            field: "creator",
		            title: "创建者",
		            align: 'center',
		            width: 100
		        }]], 
	    	 
		        }); 
          
  

  submit_button.linkbutton({ iconCls: 'icon-search', plain: true })
    .click(function () {
    	var data=form2Json(search_form);
    	if($("#ownfile").prop("checked"))
    		data.creator=$.op.code;
        listPage.datagrid({ queryParams:data});   //点击搜索
    });
  reset_button.linkbutton({ iconCls: 'icon-clear', plain: true })
  .click(function () {
	  $(':input','#'+search_form)  
      .not(':button, :submit, :reset')  
      .val(' ')  
      .removeAttr('checked')  
      .removeAttr('selected');

  });   
 
  
	//删除一条记录
  del_button.linkbutton({ iconCls: 'icon-cut', plain: true })
  .click(function () {
  	var row = listPage.datagrid('getSelected');
		if (row){
			$.messager.confirm('系统提示','确认要删除么?',function(r){
			    if (r){
			        $.get('api/fileController/del',{finalName:row.finalName},function(data){
			        	var data = JSON.parse(data);
			        	$.messager.alert("系统提示",data.msg);
			        	listPage.datagrid("reload");					        	
			        });
			    }
			});		
		}else{
			$.messager.alert("系统提示","请鼠标点击选择要删除的行！");
			return;
		}
  });
  

	 
  export_button.linkbutton({ iconCls: 'icon-print', plain: true })
 .click(function () {
	  
	 	 var url="./api/"+modelname+"/toExcel/";
     excelExport(url,listPage); 
 });      
        
  function excelExport(url,grid){
 		var queryParams = grid.datagrid('options').queryParams;
 		var params = '?1=1';
 		$.each(queryParams, function(key, val){
 			params+='&'+key+'='+val;
 		}); 
 		var page = grid.datagrid('options').pageNumber; 
 		var rows= grid.datagrid('options').pageSize; 
 		params=params+"&page="+page+"&rows="+rows;
 		var fields = '&fields=';
 		var titles='&titles=';
 		$.each(grid.datagrid('options').columns[0], function(i, val){
 			if(val.field != 'opt'){
 				fields+=val.field+',';
 				titles+=val.title+',';
 			}
 		});  
 		params=params+fields+titles;
 		window.location.href = url+ encodeURI(params);
 	}
  
//弹出 数据导入窗口
  import_button.linkbutton({ iconCls: 'icon-save', plain: true })
  .click(function () {
  	importForm.form("reset");
  	importWin.dialog("open");
  });
//保存目标Excel到数据库，关闭Excel数据导入窗口，刷新datagrid
  importButton.linkbutton({ iconCls: 'icon-ok', plain: true})
  .click(function () {
  	if (importFile.filebox('getText').length == 0){
			$.messager.alert("提示","请选择要导入的文件！");
			return;
		}		 
  	//if (checkExcelFile("excel_file",pageName+"_import_file",pageName+"_import_popwin")){
			//$.messager.alert("提示","导入Excel！");
			
			importForm.ajaxSubmit({
		        type: "POST",
		        url:"api/fileController/uploadFile",//需要修改后台
		        dataType: "text",
		        success: function(data){
		        	var data = JSON.parse(data);
		        	if(data.finalName)
		            $.messager.alert("导入结果：","导入成功");		            
		            importWin.dialog("close");
		            listPage.datagrid("reload");
		        }
		    });
			 
		//}
  });
 
function dateFormat(val, row){
		return moment(row.dkDate).format("YYYY-MM-DD");
}
//将表单数据转为json
function form2Json(id) {
    var arr = $("#" + id).serializeArray()
    var jsonStr = "";
    jsonStr += '{';
    for (var i = 0; i < arr.length; i++) {
    	if(arr[i].value!="")
        	jsonStr += '"' + arr[i].name + '":"' + arr[i].value + '",'
    }
    if(jsonStr.length>1)
    	jsonStr = jsonStr.substring(0, (jsonStr.length - 1));
    jsonStr += '}'
    var json = JSON.parse(jsonStr)
    return json
}
 $(':input','#fileup_detail_form')  
.not(':button, :submit, :reset')  
.val(' ')  
.attr('readonly','readonly');
  
}()
</script>